package cn.lingyangwl.agile.system.service.impl;

import cn.lingyangwl.agile.system.assembly.SysMicroAppAssembly;
import cn.lingyangwl.agile.system.manager.SysMicroAppManager;
import cn.lingyangwl.agile.system.mapper.SysMicroAppMapper;
import cn.lingyangwl.agile.system.model.entity.SysMicroApp;
import cn.lingyangwl.agile.system.model.rqrs.microapp.MicroAppPageReq;
import cn.lingyangwl.agile.system.model.rqrs.microapp.MicroAppResp;
import cn.lingyangwl.agile.system.model.rqrs.microapp.MicroAppSaveReq;
import cn.lingyangwl.agile.system.model.rqrs.microapp.MicroAppUpdateReq;
import cn.lingyangwl.agile.system.service.SysMicroAppService;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微应用
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class SysMicroAppServiceImpl extends ServiceImpl<SysMicroAppMapper, SysMicroApp>
    implements IService<SysMicroApp>, SysMicroAppService {

    @Resource
    private SysMicroAppManager microAppManager;
    @Resource
    private SysMicroAppAssembly microAppAssembly;
    
    @Override
    public IPage<MicroAppResp> pageMicroApp(MicroAppPageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<SysMicroApp> lqw = SysMicroApp.lqw()
            .ge(Objects.nonNull(beginTime), SysMicroApp::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), SysMicroApp::getCreatedAt, endTime)
            .orderByDesc(SysMicroApp::getCreatedAt);
        IPage<SysMicroApp> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, microAppAssembly::toMicroAppResp);
    }
    
    @Override
    public List<MicroAppResp> listMicroApp() {
    	return this.list().stream()
    	        .map(microAppAssembly::toMicroAppResp).collect(Collectors.toList());
    }

    @Override
    public MicroAppResp getMicroApp(Long id) {
        SysMicroApp entity = this.getById(id);
    	return microAppAssembly.toMicroAppResp(entity);
    }

    @Override
    public boolean saveMicroApp(MicroAppSaveReq req) {
        SysMicroApp entity = microAppAssembly.toMicroApp(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateMicroApp(MicroAppUpdateReq req) {
        SysMicroApp entity = microAppAssembly.toMicroApp(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteMicroApp(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteMicroApp(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
